package top.drinyeit.weilong.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.dialog.CenterDialog;
import top.drinyeit.weilong.presenter.ChildrenOrderDetailPresenter;

/**
 * Created by wu on 2018/1/12.
 */

public class ChildrenOrderDetailActivity extends BaseActivity {
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    private String orderId;
    public TextView consignee_name, consignee_phone, address, order_state, call;
    public RecyclerView mRecyclerView;
    public CenterDialog centerDialog;

    private ChildrenOrderDetailPresenter childrenOrderDetailPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_children_order_detail);
        setTitle(true, "订单详情");
        initInten();
        initView();
        initData();
        initListener();
    }

    private void initInten() {
        orderId = getIntent().getExtras().getString("order_id");
    }

    private void initView() {
        consignee_name = findViewById(R.id.consignee_name);
        consignee_phone = findViewById(R.id.consignee_phone);
        address = findViewById(R.id.address);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        order_state = findViewById(R.id.order_state);
        call = findViewById(R.id.tv_call);
    }

    private void initData() {
        childrenOrderDetailPresenter = new ChildrenOrderDetailPresenter(this);
        childrenOrderDetailPresenter.getCOD(orderId);
    }

    private void initListener() {
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                centerDialog = new CenterDialog(ChildrenOrderDetailActivity.this, R.layout.dialog_center);
                centerDialog.show();
                TextView textView = centerDialog.findViewById(R.id.dialog_text);
                textView.setText("183 5774 7501");

                TextView textView1 = centerDialog.findViewById(R.id.dialog_sure);
                textView1.setText("取消");
                textView1.setTextColor(getResources().getColor(R.color.red));
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        centerDialog.dismiss();
                    }
                });

                TextView textView2 = centerDialog.findViewById(R.id.dialog_cancel);
                textView2.setText("呼叫");
                textView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        call("18357747501");
                        finish();
                    }
                });
            }
        });
    }

    /**
     * 调用拨号功能
     *
     * @param phone 电话号码
     */
    private void call(String phone) {
        // 检查是否获得了权限（Android6.0运行时权限）
        if (ContextCompat.checkSelfPermission(ChildrenOrderDetailActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // 没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(ChildrenOrderDetailActivity.this,
                    Manifest.permission.CALL_PHONE)) {
                // 返回值：
//                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                // 弹窗需要解释为何需要该权限，再次请求授权
                Toast.makeText(ChildrenOrderDetailActivity.this, "请授权！", Toast.LENGTH_LONG).show();

                // 帮跳转到该应用的设置界面，让用户手动授权
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            } else {
                // 不需要解释为何需要该权限，直接请求授权
                ActivityCompat.requestPermissions(ChildrenOrderDetailActivity.this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        MY_PERMISSIONS_REQUEST_CALL_PHONE);
            }
        } else {
            // 已经获得授权，可以打电话
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
            startActivity(intent);
        }

    }

    // 处理权限申请的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 授权成功，继续打电话
                    // 拨号：激活系统的拨号组件
                    Intent intent = new Intent(); // 意图对象：动作 + 数据
                    intent.setAction(Intent.ACTION_CALL); // 设置动作
                    Uri data = Uri.parse("tel:" + "18357747501"); // 设置数据
                    intent.setData(data);
                    startActivity(intent); // 激活Activity组件
                } else {
                    // 授权失败！
                    Toast.makeText(this, "授权失败！", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }

    }
}
